<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_UserController extends Zend_Controller_Action
{

	public function init(){
		 $this->Userupdatemess="";
	}

    public function indexAction()
    {
       Zend_Loader::loadFile("../application/admin/models/User.php");
         $this->_helper->layout->setLayout('admin_layout');
         $request = $this->getRequest();
       $User=new User();
       $Userarr=$User->listUsers();
       $stat = $request->getParam('stat');
     //  print_r($Userarr);exit;
       $this->view->stat = $stat;
       $this->view->Userarr=$Userarr;
    }
       public function addAction()
    {
        Zend_Loader::loadFile('../application/admin/forms/User.php');
       $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $User_form = new Form_Userform($options);
        $this->view->User_form = $User_form;
        $this->view->title = "Edit";

    }
       public function editAction()
    {
        Zend_Loader::loadFile("../application/admin/models/User.php");
        Zend_Loader::loadFile("../application/admin/forms/User.php");
        $Usermodel=new User();
        $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $admin_id = $this->_getParam('admin_id', 0);
        $options = array("action"=>$action, "admin_id"=>$admin_id);
        $Userform = new Form_Userform($options);
        $this->view->User_form =  $Userform;
        $this->view->admin_id = $admin_id;
        if ($admin_id > 0) {
            $Userform->populate($Usermodel->getUser($admin_id));
        }      
        $Userdata=$Usermodel->getUser($admin_id);
 

    }

     public function doaddAction()
    {
        Zend_Loader::loadFile("../application/admin/models/User.php");
        Zend_Loader::loadFile("../application/admin/forms/User.php");
       print_r($_POST);
         $request = $this->getRequest();
        $modUser=new User();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $Userform=new Form_Userform($options);
        if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($Userform->isValid($formdata)){
                    $insertarr["admin_firstname"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_firstname') );
                    $insertarr["admin_lastname"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_lastname') );
                    $insertarr["admin_email"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_email') );
                    $insertarr["admin_username"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_username') );
                    $insertarr["admin_password"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_password') );
                    $insertarr["admin_userType"]=preg_replace ( "/'/", "\\'",$request->getParam('admin_userType') );
                    $insertarr["admin_status"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_status') );
                    
                         $addUser=$modUser->addUser($insertarr)  ;
                    

                    if($addUser)
                    {
                         $this->_redirect('/admin/User/index/stat/updated');
                         $this->Userupdatemess="Successfully added!";

                    }
              }
              else{
                   $Userform->populate($formdata);
                   $this->_helper->layout->setLayout('admin_layout');
                   $this->view->User_form = $Userform;
              }
            
            }
            
    }

    public function doeditAction()
    {
        Zend_Loader::loadFile("../application/admin/models/User.php");
        Zend_Loader::loadFile("../application/admin/forms/User.php");
      // print_r($_POST);
        $modUser=new User();
        
        $request = $this->getRequest();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $Userform=new Form_Userform($options);
        
         if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($Userform->isValid($formdata)){
                    $insertarr["admin_id"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_id') );
                    $insertarr["admin_firstname"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_firstname') );
                    $insertarr["admin_lastname"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_lastname') );
                    $insertarr["admin_email"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_email') );
                    $insertarr["admin_username"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_username') );
                    $insertarr["admin_password"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_password') );
                    $insertarr["admin_userType"]=preg_replace ( "/'/", "\\'",$request->getParam('admin_userType') );
                    $insertarr["admin_status"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_status') );
                $editUser=$modUser->editUser($insertarr)  ;
                if($editUser)
                {
                     //$this->_redirect('admin/User/edit/id/'.$insertarr["id"]);
                    $this->_redirect('admin/User/index/stat/updated');
                    $this->Userupdatemess="Successfully updated!";
                }
            }
            else{
                $Userform->populate($formdata);
                $this->_helper->layout->setLayout('admin_layout');
                $this->view->Userform = $Userform;
            }

             $this->_redirect('admin/User/index/stat/updated');
         }
        
    }

     public function deleteAction()
    {
        Zend_Loader::loadFile("../application/admin/models/User.php");
      // print_r($_POST);
        $modUser=new User();
        $request = $this->getRequest();
        $admin_id=$request->getParam('admin_id');
        $deleteUser=$modUser->deleteUser($admin_id);
        if($deleteUser)
        {
             $this->_redirect('admin/User/index/stat/deleted');
        }
     }
     public function changestatusAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/User.php");
		$modUser = new User();
		$request = $this->getRequest();
		$admin_id = $request->getParam('admin_id');
		$modUser->changeUserStatus($admin_id);
                exit;
	}

   public function getcontentAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/User.php");
		$modUser = new User();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		echo $modUser->getcontent($id);
	}




}
?>
